<?php

namespace Analysis\Model;

use Think\Model;

class GroupuserModel extends Model
{
    protected $_validate = array(
        array('gro_name','','帐号名称已经存在',0,'unique',1),
        array('gro_pass','verifyOldPass','旧密码错误/新密码不一致',0,'callback',2),
        array('gro_type', 'checkReturnVal', '请选择正确的分组模式', 0, 'callback',1),
        array('gro_status', 'checkReturnVal', '用户状态选择错误', 0, 'callback',1)
    );
    
    protected $_auto = array(
        array("gro_pass", "getNewPass", 3, "callback"),
        array('gro_time', 'time', 3, 'function'),
    );
    
    protected function checkReturnVal($data)
    {
        return $data == 0 ? false : true;
    }
    
    protected function verifyOldPass()
    {
        $groupuser=M("groupuser");
        if (I("post.gro_old")!=null) {
            $info=$groupuser->find(I('get.id'));
            if ($info["gro_pass"]!=$this->enCode(I("post.gro_old"))) {
                return false;
            }
            if (I("post.gro_pass")!=I("post.gro_vinew")) {
                return false;
            }
        }
        return true;
    }


    protected function getNewPass($data)
    {
        $groupuser=M("groupuser");
        if ($data==null) {
            $info=$groupuser->find(I('get.id'));
            return $info["gro_pass"];
        } else {
            return $this->enCode($data);
        }
    }
    
    private function enCode($val)
    {
        $temp=trim(md5($val));
        return substr($temp, 12, 32);
    }
}
